package top.kaoshanji.p1ch02c;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02c-06：MethodHandle#invokeExact的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/2 18:37
 */
public class MethodHandleExample01 {

    private static final Logger logger = LogManager.getLogger(MethodHandleExample01.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02c-06：MethodHandle#invokeExact的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType methodType = MethodType.methodType(String.class, int.class, int.class);

        // 例如，调用 String#substring
        MethodHandle methodHandle = lookup.findVirtual(String.class, "substring", methodType);
        String str = (String) methodHandle.invokeExact("Hello World", 1, 3);
        System.out.println(str);

    }

}
